package com.dudu.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.IvrDailRequestBean;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.ICheckValidityService;
import com.dudu.service.IIvrNoticesService;
import com.dudu.service.ILogService;
import com.dudu.service.IOperationPlatformService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.CheckValidityServiceImpl;
import com.dudu.service.impl.IvrNoticesServcieImpl;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.OperationPlatformServiceImpl;
import com.dudu.utils.DateUtils;
import com.dudu.utils.PhoneUtils;

import core.CoreService;

/**
 * IVR外呼
 * 
 * @author gaoxs
 * @date 2016-9-06
 * 
 */
public class OpenAbilityIvrDialController extends CoreService implements IOpenAbilityIvrDialController {
    private ILogService logService = new LogService();
    // app扩展信息操作
    private IAppExtendService appExtendService = new AppExtendServiceImpl();

    private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
    // 验证信息类
    private ICheckValidityService checkValidityService = new CheckValidityServiceImpl();
    // 运营平台相关操作
    private IOperationPlatformService operationPlatformService = new OperationPlatformServiceImpl();
    private IIvrNoticesService ivrNoticesService = new IvrNoticesServcieImpl();
    // 被叫人数限定
    private int calleeNum = 51;

    /**
     * 多按键 语音调研接口
     * 
     * @return
     */
    public String IvrDailMultipleKey() {
	log.info("load SendIvrDailMultipleKey");
	long begin = Calendar.getInstance().getTimeInMillis();
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 接口请求参数
	// app_key应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 发起语音短信的用户ID
	String ext_terminalCode = this.getRequest().getParameter("ext_terminalCode");
	String[] ext_terminalCodeStr = { "ext_terminalCode", ext_terminalCode, "50", "0", "str" };
	checkParamlist.add(ext_terminalCodeStr);
	// 主叫号码
	String caller = this.getRequest().getParameter("caller");
	String[] callerStr = { "caller", caller, "20", "0", "str" };
	checkParamlist.add(callerStr);
	// 前导音
	String tts_content = this.getRequest().getParameter("tts_content");
	String[] tts_contentStr = { "tts_content", tts_content, "200", "1", "str" };
	checkParamlist.add(tts_contentStr);

	// 语音调研内容
	String content_data = this.getRequest().getParameter("content_data");
	String[] content_dataStr = { "content_data", content_data, "35000", "0", "jsonArray" };
	checkParamlist.add(content_dataStr);
	// 预约发起短信时间
	String schedule_send_time = this.getRequest().getParameter("schedule_send_time");
	String[] schedule_send_timeStr = { "schedule_send_time", schedule_send_time, "50", "1", "date" };
	checkParamlist.add(schedule_send_timeStr);
	if (StringUtils.isBlank(schedule_send_time)) {
	    schedule_send_time = "";
	}
	// 被叫信息
	String called_data = this.getRequest().getParameter("called_data");
	String[] called_dataStr = { "called_data", called_data, "35000", "0", "jsonArray" };
	checkParamlist.add(called_dataStr);
	// 批次号
	String batch_number = this.getRequest().getParameter("batch_number");
	String[] batch_numberStr = { "batch_number", batch_number, "100", "1", "str" };
	checkParamlist.add(batch_numberStr);
	if (StringUtils.isBlank(batch_number)) {
	    batch_number = "";
	}
	// ISV自行定义使用哪个号码发起通话
	String pool_ability_id = this.getRequest().getParameter("pool_ability_id");
	String[] pool_ability_idStr = { "pool_ability_id", pool_ability_id, "20", "1", "str" };
	checkParamlist.add(pool_ability_idStr);
	// 判断如果号码参数为空未收到参数，则处理为空字符串
	if (StringUtils.isBlank(pool_ability_id)) {
	    pool_ability_id = "";
	}
	// 强制使用平台号码(值为1时，强制使用平台号码发起)
	String usePlatNbr = this.getRequest().getParameter("usePlatNbr");
	String[] usePlatNbrStr = { "usePlatNbr", usePlatNbr, "20", "1", "str" };
	checkParamlist.add(usePlatNbrStr);
	if (StringUtils.isBlank(usePlatNbr) || !"1".equals(usePlatNbr)) { // 不为1时不强制使用平台号
	    usePlatNbr = "";
	}
	String checkcalled=this.getRequest().getParameter("checkcalled");//默认:0 验证 1不验证
	String[] checkcalledStr = { "checkcalled", checkcalled, "20", "1", "str" };
	checkParamlist.add(checkcalledStr);
	if (StringUtils.isBlank(checkcalled) ) { // 不为1时不强制使用平台号
	    checkcalled = "1";
	}
	// 适配jsonp
	String callback = this.getRequest().getParameter("callback");
	JSONObject object = null;
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		org.json.JSONArray callA = new org.json.JSONArray(called_data);
		JSONArray calledDataJson = JSONArray.fromObject(callA.toString());

		if (calledDataJson != null && calledDataJson.size() <= 100) {
		    // 查询应用信息
		    AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		    // 判断app信息是否为空
		    if (appExtendBean != null) {
			// 根据cust_account查询账号信息
			AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
			// 判断账号信息是否找到
			if (accountBean != null) {
			    // 验证秘钥信息
			    if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
				// 获取语音短信能力类型标识
				String abilityType = AbilityTypeEnum.IVVR.getKey();
				// 到运营平台验证账号相关信息--判断是否可以通话
				if (operationPlatformService.platformAuthentication(object, appExtendBean, accountBean, abilityType)) {
				    IvrDailRequestBean requestBean = new IvrDailRequestBean();
				    requestBean.setApp_key(app_key);// app_key应用标识
				    requestBean.setPeriphery_code(appExtendBean.getPeriphery_code());// 应用外系统编码
				    requestBean.setCust_account(cust_account);// cust_account账号标识
				    requestBean.setUserIdentify(ext_terminalCode);// 发起语音短信的用户ID
				    requestBean.setCaller(caller);// 主叫
				    requestBean.setSchedule_send_time(schedule_send_time);// 预药时间
				    requestBean.setPool_ability_id(pool_ability_id);// ISV自行定义使用哪个号码发起通话
				    requestBean.setBatch_number(batch_number);// 批次号
				    requestBean.setUsePlatNbr(usePlatNbr);// 强制使用平台号码(值为1时，强制使用平台号码发起)
				    requestBean.setTts_content(tts_content);// 前导音
				    requestBean.setCalled_data(calledDataJson);// 被叫信息
				    org.json.JSONArray contA = new org.json.JSONArray(content_data);
				    requestBean.setContent_data(JSONArray.fromObject(contA.toString()));
				    requestBean.setCheckcalled(checkcalled);
				    if (StringUtils.isNotBlank(tts_content) && tts_content.split("\\{").length - 1 > 5) {
					object.put("result", "-1");
					object.put("describe", "前导音占位符超过5个");
					object.put("code", "0516");
				    } else {
					object = ivrNoticesService.checkIvrDial(requestBean);
					if (object.optString("result").equals("0")) {
					    object = ivrNoticesService.IvrDialNotice(requestBean, "2");
					}

				    }

				} else {
				    object.put("result", "-1");
				    object.put("code", object.getString("code"));
				    object.put("describe", object.getString("describe"));
				}
			    } else {
				object.put("result", "-1");
				object.put("describe", "参数sign错误");
				object.put("code", "0001");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "计费账号不存在");
			    object.put("code", "0002");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "请使用正确的应用标识");
			object.put("code", "0007");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "单次提交的手机号码数量不能超过100人");
		    object.put("code", "0502");
		}

	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.info("********IVR外呼失败**********" + e);
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("describe", "语音调研异常");
	    object.put("errorMsg", e.getMessage());
	    object.put("code", "0501");
	}
	// 计算结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 计算发起接口道调用完毕所用的时间
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼写写入日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_terminalCode:" + ext_terminalCode
		+ "|caller:" + caller + "|tts_content:" + tts_content + "|content_data:" + content_data + "|schedule_send_time:" + schedule_send_time + "|called_data:"
		+ called_data + "|batch_number:" + batch_number + "|pool_ability_id:" + pool_ability_id + "|usePlatNbr:" + usePlatNbr;
	this.logService.logInterface(during, "IvrDailMultipleKeyV4", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), caller);

	String data = "";
	object.remove("errorMsg");
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

    /**
     * 金山税所IVR语音调研并按9发短信
     * 
     * @return
     */
    public String IvrDailBusiness() {
	log.info("load SendIvrDailBusiness");
	long begin = Calendar.getInstance().getTimeInMillis();
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 接口请求参数
	// app_key应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 发起语音短信的用户ID
	String ext_terminalCode = this.getRequest().getParameter("ext_terminalCode");
	String[] ext_terminalCodeStr = { "ext_terminalCode", ext_terminalCode, "50", "0", "str" };
	checkParamlist.add(ext_terminalCodeStr);
	// 主叫号码
	String caller = this.getRequest().getParameter("caller");
	String[] callerStr = { "caller", caller, "20", "0", "str" };
	checkParamlist.add(callerStr);
	// 前导音
	String tts_content = this.getRequest().getParameter("tts_content");
	String[] tts_contentStr = { "tts_content", tts_content, "200", "1", "str" };
	checkParamlist.add(tts_contentStr);

	// 发送语音名称
	String mediaName = this.getRequest().getParameter("mediaName");
	String[] mediaNameStr = { "mediaName", mediaName, "50", "0", "str" };
	checkParamlist.add(mediaNameStr);
	// 预约发起短信时间
	String schedule_send_time = this.getRequest().getParameter("schedule_send_time");
	String[] schedule_send_timeStr = { "schedule_send_time", schedule_send_time, "50", "1", "date" };
	checkParamlist.add(schedule_send_timeStr);
	if (StringUtils.isBlank(schedule_send_time)) {
	    schedule_send_time = "";
	}
	// 被叫信息
	String called_data = this.getRequest().getParameter("called_data");
	String[] called_dataStr = { "called_data", called_data, "35000", "0", "jsonArray" };
	checkParamlist.add(called_dataStr);
	// 签名内容：3至8个字符长度
	String sms_signature = this.getRequest().getParameter("sms_signature").replaceAll("\r|\n|\t", "");
	String[] sms_signatureStr = { "sms_signature", sms_signature, "8", "0", "str" };
	checkParamlist.add(sms_signatureStr);
	// 后续接口类型接口类型:1:短信,2:回呼 默认为1
	String callback_interface_type = this.getRequest().getParameter("callback_interface_type");
	String[] callback_interface_typeStr = { "callback_interface_type", callback_interface_type, "50", "1", "str" };
	checkParamlist.add(callback_interface_typeStr);
	if (StringUtils.isBlank(callback_interface_type)) {
	    callback_interface_type = "1";
	}
	// 按键 0~9 默认9
	String digit = this.getRequest().getParameter("digit");
	String[] digitStr = { "digit", digit, "1", "1", "str" };
	checkParamlist.add(digitStr);
	if (StringUtils.isBlank(digit)) {
	    digit = "9";
	}
	// 按键内容 示例: 按9获取语音文字内容
	String digit_content = this.getRequest().getParameter("digit_content");
	String[] digit_contentStr = { "digit_content", digit_content, "30", "1", "str" };
	checkParamlist.add(digit_contentStr);
	if (StringUtils.isBlank(digit_content)) {
	    digit_content = "按9获取语音文字内容";
	}
	// 批次号
	String batch_number = this.getRequest().getParameter("batch_number");
	String[] batch_numberStr = { "batch_number", batch_number, "100", "1", "str" };
	checkParamlist.add(batch_numberStr);
	if (StringUtils.isBlank(batch_number)) {
	    batch_number = "";
	}
	// ISV自行定义使用哪个号码发起通话
	String pool_ability_id = this.getRequest().getParameter("pool_ability_id");
	String[] pool_ability_idStr = { "pool_ability_id", pool_ability_id, "20", "1", "str" };
	checkParamlist.add(pool_ability_idStr);
	// 判断如果号码参数为空未收到参数，则处理为空字符串
	if (StringUtils.isBlank(pool_ability_id)) {
	    pool_ability_id = "";
	}
	// 强制使用平台号码(值为1时，强制使用平台号码发起)
	String usePlatNbr = this.getRequest().getParameter("usePlatNbr");
	String[] usePlatNbrStr = { "usePlatNbr", usePlatNbr, "20", "1", "str" };
	checkParamlist.add(usePlatNbrStr);
	if (StringUtils.isBlank(usePlatNbr) || !"1".equals(usePlatNbr)) { // 不为1时不强制使用平台号
	    usePlatNbr = "";
	}
	String checkcalled=this.getRequest().getParameter("checkcalled");//默认:0 验证 1不验证
	String[] checkcalledStr = { "checkcalled", checkcalled, "20", "1", "str" };
	checkParamlist.add(checkcalledStr);
	if (StringUtils.isBlank(checkcalled) ) { 
	    checkcalled = "1";
	}
	// 适配jsonp
	String callback = this.getRequest().getParameter("callback");
	JSONObject object = null;
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		org.json.JSONArray callA = new org.json.JSONArray(called_data);
		JSONArray calledDataJson = JSONArray.fromObject(callA.toString());
		// JSONArray calledDataJson = JSONArray.fromObject(called_data);
		if (calledDataJson != null && calledDataJson.size() <= 100) {
		    // 查询应用信息
		    AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		    // 判断app信息是否为空
		    if (appExtendBean != null) {
			// 根据cust_account查询账号信息
			AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
			// 判断账号信息是否找到
			if (accountBean != null) {
			    // 验证秘钥信息
			    if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
				// 获取语音短信能力类型标识
				String abilityType = AbilityTypeEnum.IVVR.getKey();
				// 到运营平台验证账号相关信息--判断是否可以通话
				if (operationPlatformService.platformAuthentication(object, appExtendBean, accountBean, abilityType)) {
				    IvrDailRequestBean requestBean = new IvrDailRequestBean();
				    requestBean.setApp_key(app_key);// app_key应用标识
				    requestBean.setPeriphery_code(appExtendBean.getPeriphery_code());// 应用外系统编码
				    requestBean.setCust_account(cust_account);// cust_account账号标识
				    requestBean.setUserIdentify(ext_terminalCode);// 发起语音短信的用户ID
				    requestBean.setCaller(caller);// 主叫
				    requestBean.setMediaName(mediaName.trim());// 语音文件名
				    requestBean.setSchedule_send_time(schedule_send_time);// 预药时间
				    requestBean.setPool_ability_id(pool_ability_id);// ISV自行定义使用哪个号码发起通话
				    requestBean.setBatch_number(batch_number);// 批次号
				    requestBean.setUsePlatNbr(usePlatNbr);// 强制使用平台号码(值为1时，强制使用平台号码发起)
				    requestBean.setTts_content(tts_content);// 前导音
				    requestBean.setCalled_data(calledDataJson);// /
									       // 被叫信息
				    requestBean.setSms_signature(sms_signature);// 签名内容：3至8个字符长度
				    requestBean.setCallback_interface_type(callback_interface_type);// 后续接口类型接口类型:1:短信,2:回呼
												    // 默认为1
				    requestBean.setDigit(digit);// 按键 0~9 默认9
				    requestBean.setDigit_content(digit_content);// 按键内容
										// 示例:
										// 按9获取语音文字内容
				    requestBean.setCheckcalled(checkcalled);
				    if (StringUtils.isNotBlank(tts_content) &&  tts_content.split("\\{").length - 1 > 5) {
					object.put("result", "-1");
					object.put("describe", "前导音占位符超过5个");
					object.put("code", "0516");
				    } else {
					object = ivrNoticesService.checkIvrDial(requestBean);
					if (object.optString("result").equals("0")) {
					    object = ivrNoticesService.IvrDialNotice(requestBean, "1");
					}

				    }

				} else {
				    object.put("result", "-1");
				    object.put("code", object.getString("code"));
				    object.put("describe", object.getString("describe"));
				}
			    } else {
				object.put("result", "-1");
				object.put("describe", "参数sign错误");
				object.put("code", "0001");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "计费账号不存在");
			    object.put("code", "0002");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "请使用正确的应用标识");
			object.put("code", "0007");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "单次提交的手机号码数量不能超过100人");
		    object.put("code", "0502");
		}

	    }
	} catch (Exception e) {
	    log.info("********IVR外呼失败**********" + e);
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("describe", "语音调研异常");
	    object.put("errorMsg", e.getMessage());
	    object.put("code", "0501");
	}
	// 计算结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 计算发起接口道调用完毕所用的时间
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼写写入日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_terminalCode:" + ext_terminalCode
		+ "|caller:" + caller + "|tts_content:" + tts_content + "|mediaName:" + mediaName + "|schedule_send_time:" + schedule_send_time + "|called_data:" + called_data
		+ "|sms_signature:" + sms_signature + "|callback_interface_type:" + callback_interface_type + "|digit:" + digit + "|digit_content:" + digit_content
		+ "|batch_number:" + batch_number + "|pool_ability_id:" + pool_ability_id + "|usePlatNbr:" + usePlatNbr;
	this.logService.logInterface(during, "IvrDailBusinessV4", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), caller);

	String data = "";
	object.remove("errorMsg");
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

    /**
     * IVR 批量外呼
     * 
     * @return
     */
    public String IvrDial() {
	log.info("load SendIvrDial");
	long begin = Calendar.getInstance().getTimeInMillis();
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 接口请求参数
	// app_key应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 发起语音短信的用户ID
	String ext_terminalCode = this.getRequest().getParameter("ext_terminalCode");
	String[] ext_terminalCodeStr = { "ext_terminalCode", ext_terminalCode, "50", "0", "str" };
	checkParamlist.add(ext_terminalCodeStr);
	// 主叫号码
	String caller = this.getRequest().getParameter("caller");
	String[] callerStr = { "caller", caller, "20", "0", "str" };
	checkParamlist.add(callerStr);
	// 被叫号码：以‘,’分割
	String called = this.getRequest().getParameter("called");
	String[] calledStr = { "called", called, "1000", "0", "str" };
	checkParamlist.add(calledStr);
	// 文字信息可为空
	String content = this.getRequest().getParameter("content");
	String[] contentStr = { "content", content, "1000", "1", "str" };
	checkParamlist.add(contentStr);
	// 语音文件名称可为空
	String mediaName = this.getRequest().getParameter("mediaName");
	String[] mediaNameStr = { "mediaName", mediaName, "50", "1", "str" };
	checkParamlist.add(mediaNameStr);
	// 按键个数
	String recvDigits = this.getRequest().getParameter("recvDigits");
	String[] recvDigitsStr = { "recvDigits", recvDigits, "10", "1", "str" };
	checkParamlist.add(recvDigitsStr);
	if (StringUtils.isBlank(recvDigits)) {
	    recvDigits = "1";
	}
	// 发送开始时间
	String startDate = this.getRequest().getParameter("startDate");
	String[] startDateStr = { "startDate", startDate, "50", "1", "date" };
	checkParamlist.add(startDateStr);
	// 发送结束时间
	String endDate = this.getRequest().getParameter("endDate");
	String[] endDateStr = { "endDate", endDate, "50", "1", "date" };
	checkParamlist.add(endDateStr);
	if (StringUtils.isBlank(endDate)) {
	    endDate = "";
	}
	// 语音通信预约后，通信发起sessionid接收地址
	String push_url = this.getRequest().getParameter("push_url");
	String[] push_urlStr = { "push_url", push_url, "1000", "1", "str" };
	checkParamlist.add(push_urlStr);
	if (StringUtils.isBlank(push_url)) {
	    push_url = "";
	}
	// ISV自行定义使用哪个号码发起通话
	String pool_ability_id = this.getRequest().getParameter("pool_ability_id");
	String[] pool_ability_idStr = { "pool_ability_id", pool_ability_id, "20", "1", "str" };
	checkParamlist.add(pool_ability_idStr);
	// 判断如果号码参数为空未收到参数，则处理为空字符串
	if (StringUtils.isBlank(pool_ability_id)) {
	    pool_ability_id = "";
	}
	// 强制使用平台号码(值为1时，强制使用平台号码发起)
	String usePlatNbr = this.getRequest().getParameter("usePlatNbr");
	String[] usePlatNbrStr = { "usePlatNbr", usePlatNbr, "20", "1", "str" };
	checkParamlist.add(usePlatNbrStr);
	if (StringUtils.isBlank(usePlatNbr) || !"1".equals(usePlatNbr)) { // 不为1时不强制使用平台号
	    usePlatNbr = "";
	}
	// 是否为一批次
	String batch_number = this.getRequest().getParameter("batch_number");
	String[] batch_numberStr = { "batch_number", batch_number, "100", "1", "str" };
	checkParamlist.add(batch_numberStr);
	if (StringUtils.isBlank(batch_number)) {
	    batch_number = "";
	}
	// 适配jsonp
	String callback = this.getRequest().getParameter("callback");
	JSONObject object = null;
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 判断手机号码是否正确，过滤外国号码等
		if (PhoneUtils.filterIssueNumber(caller) && PhoneUtils.filterIssueNumber(called)) {
		    // 处理重复的电话号码
		    called = PhoneUtils.phoneNumDereplication(called);
		    // 被叫人数限定
		    String[] calleeStr = called.split(",");
		    if (calleeStr != null && calleeStr.length < calleeNum) {
			// 查询应用信息
			AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
			// 判断app信息是否为空
			if (appExtendBean != null) {
			    // 根据cust_account查询账号信息
			    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
			    // 判断账号信息是否找到
			    if (accountBean != null) {
				// 验证秘钥信息
				if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
				    // 获取语音短信能力类型标识
				    String abilityType = AbilityTypeEnum.IVVR.getKey();
				    // 到运营平台验证账号相关信息--判断是否可以通话
				    if (operationPlatformService.platformAuthentication(object, appExtendBean, accountBean, abilityType)) {
					// 发起留言
					IvrDailRequestBean requestBean = new IvrDailRequestBean();
					requestBean.setApp_key(app_key);// app_key应用标识
					requestBean.setPeriphery_code(appExtendBean.getPeriphery_code());// 应用外系统编码
					requestBean.setCust_account(cust_account);// cust_account账号标识
					requestBean.setUserIdentify(ext_terminalCode);// 发起语音短信的用户ID
					requestBean.setCaller(caller);// 主叫
					requestBean.setCalled(called);// 被叫
					requestBean.setContent(content);// 内容
					requestBean.setMediaName(mediaName.trim());// 语音文件名
					requestBean.setRecvDigits(recvDigits);// 按键个数
					requestBean.setSchedule_send_time(startDate);// 预药时间
					requestBean.setEndDate(endDate);// 结束时间
					requestBean.setPush_url(push_url);// 推送sessionId
									  // URL
					requestBean.setPool_ability_id(pool_ability_id);// ISV自行定义使用哪个号码发起通话
					requestBean.setBatch_number(batch_number);// 批次号
					requestBean.setUsePlatNbr(usePlatNbr);// 强制使用平台号码(值为1时，强制使用平台号码发起)
					object = ivrNoticesService.IvrDialNotice(requestBean, "0");
				    } else {
					object.put("result", "-1");
					object.put("code", object.getString("code"));
					object.put("describe", object.getString("describe"));
				    }
				} else {
				    object.put("result", "-1");
				    object.put("describe", "参数sign错误");
				    object.put("code", "0001");
				}
			    } else {
				object.put("result", "-1");
				object.put("describe", "计费账号不存在");
				object.put("code", "0002");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "请使用正确的应用标识");
			    object.put("code", "0007");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "单次提交的手机号码数量不能超过" + calleeNum + "人");
			object.put("code", "0502");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "号码格式错误");
		    object.put("code", "0004");
		}
	    }
	} catch (Exception e) {
	    log.info("********IVR外呼失败**********" + e);
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("describe", "语音调研异常");
	    object.put("errorMsg", e.getMessage());
	    object.put("code", "0501");
	}
	// 计算结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 计算发起接口道调用完毕所用的时间
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼写写入日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_terminalCode:" + ext_terminalCode
		+ "|caller:" + caller + "|called:" + called + "|content:" + content + "|mediaName:" + mediaName + "|recvDigits:" + recvDigits + "|startDate:" + startDate
		+ "|endDate:" + endDate + "|push_url:" + push_url + "|usePlatNbr:" + usePlatNbr + "|batch_number:" + batch_number + "|pool_ability_id:" + pool_ability_id;
	this.logService.logInterface(during, "IvrDialV4", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(),
		this.getRequest().getRemoteAddr(), caller);

	String data = "";
	object.remove("errorMsg");
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

}
